package com.xjw.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.xjw.entity.User;
import com.xjw.service.UserService;

/**      
 * 项目名称：springMvc  
 * 实现功能：  
 * 类名称：UserController   
 * 类描述：(该类的主要功能)
 * 创建人：徐纪伟 
 * E-mail: xjw@jysdsoft.com
 * 创建时间：2013-3-22上午09:10:46   
 * 修改人：   
 * 修改时间：   
 * 版权 :京源时代
 * 修改备注：   
 * @version    
 */
@Controller
@RequestMapping(value="/user")//url统一目录路径
public class UserController {

	@Resource
	//@Autowired
	private UserService userService;
	
	
	@RequestMapping(value="/create.do")//子路径
	public ModelAndView create(HttpServletRequest request, HttpServletResponse response, User user) {
		if("GET".equals(request.getMethod())) {
		//如果是get请求 我们转向 新增页面
			ModelAndView mv = new ModelAndView();
			mv.addObject("flag", "add");
			mv.setViewName("user/create");
			return mv;
		}
		userService.create(user);
		//直接重定向到列表页面
		return new ModelAndView("redirect:/user/list.do");
	
	}
	@RequestMapping(value="/update.do")
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response, User user) {
			if("GET".equals(request.getMethod())) {
			//如果是get请求 我们转向更新页面
			ModelAndView mv = new ModelAndView();
			//查询要更新的数据
			mv.addObject("user", userService.get(user.getUsername()));
			mv.addObject("flag", "update");
			mv.setViewName("user/create");
			return mv;
			}
			userService.update(user);
			//直接重定向到列表页面
			return new ModelAndView("redirect:/user/list.do");
	}
	
	@RequestMapping(value="/delete.do")
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, User user) {
			userService.delete(user);
			//直接重定向到列表页面
			return new ModelAndView("redirect:/user/list.do");
	}
	
	//参数映射方法，一个controller对应一个url
	//如果URL请求中包括"method=list"的参数，由本方法进行处理
    //@RequestMapping(params = "method=list") 
	//每个url对应一个方法，方法上指定url的value
	@RequestMapping(value="/list.do")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("userList", userService.list());
		mv.setViewName("user/list");
		return mv;
	}
	
	@RequestMapping(value = "/jsonTest1.do")  
	@ResponseBody
	public Map<String, String> getMap(HttpServletRequest request, HttpServletResponse response) {  
		response.setContentType("text/plain;charset=UTF-8");
		HashMap<String, String> hsm = new HashMap<String, String>();  
        hsm.put("a", "你好");  
        hsm.put("b", "1你好");  
        return hsm;  
	}  
	
	@RequestMapping(value = "/jsonTest2.do") 
	@ResponseBody
	public String getJsonString() {  
		String json = "{{'a':1},{'b',2}}";
        return json;  
	}  
	
	@RequestMapping(value = "/uploadSingleFile.do")
	public String uploadSingleFile(MultipartFile file, HttpServletRequest request) throws IOException{
		String path =  request.getSession().getServletContext().getRealPath("/WEB-INF/upload"); 
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,file.getOriginalFilename()));
		System.out.println(file.getOriginalFilename());
		return "user/upload";
	}
	
	@RequestMapping(value = "/uploadMultFile.do")
	public String uploadMultFile(@RequestParam MultipartFile[] files, HttpServletRequest request) throws IOException{
		String path =  request.getSession().getServletContext().getRealPath("/WEB-INF/upload"); 
		for(MultipartFile file : files){
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,file.getOriginalFilename()));
			System.out.println(file.getOriginalFilename());
		}
		
		return "user/upload";
	}
 


}
